/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      CreateProcessInstanceCommand.java
 **  Description:    Starts a new process by creating a new process instance and a task instance. 
 **                  Making use of the existing methods and attributes of parent class.
 **  Date:           2008-10-27
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import java.util.Date;

import org.jbpm.JbpmContext;
import org.jbpm.command.NewProcessInstanceCommand;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.ti.andon.workflow.WFConstants;

public class CreateProcessInstanceCmd extends NewProcessInstanceCommand
{

	/**
	 * Serial Version ID.
	 */
	private static final long serialVersionUID = 3202813720592996339L;

	/**
	 * Creates a new process instance and a task instance, and triggers a signal on the token.
	 * 
	 * @param jbpmContext jbpm context
	 * @param businessKey business model ID
	 * @param transitionName the name of transition to be triggered
	 * @param requester the requester id
	 * @param projectType the budget project type
	 * @param budgetResDepartment the budget responsibility department
	 * @param budgetAmount the budget amount
	 * @param budgetAdjAmount the adjustment amount of budget.
	 * @param draftTime the draft time
	 * @return the process instance created and triggers a signal on the token.
	 * 
	 * @throws Exception
	 */
	public Object execute(JbpmContext jbpmContext, String businessKey, String transitionName, Long requester,
			Long budgetResDepartment, Date draftTime)
			throws Exception
	{
		if (null == businessKey)
		{
			throw new Exception("CreateProcessInstanceCmd.execute() : businessKey can not be null!!!");
		}
		this.setCreateStartTask(true);
		if (this.getActorId() != null)
		{
			jbpmContext.setActorId(this.getActorId());
		}

		// Creates a new process instance for the latest version of the process definition with the given name.
		ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition(
				this.getProcessName());
		ProcessInstance processInstance = processDefinition.createProcessInstance();

		// Sets variables.
		ContextInstance contextInstance = processInstance.getContextInstance();
		contextInstance.setVariable(WFConstants.PROCESS_NAME, WFConstants.processNameAbbr
				.getProperty(this.getProcessName()));
		if (null != requester)
		{
			contextInstance.setVariable(WFConstants.REQUESTER, requester);
		}
		
		if (null != budgetResDepartment)
		{
//			contextInstance.setVariable(WFConstants.BUDGET_RES_DEPARTMENT, budgetResDepartment);
		}
		contextInstance.setVariable(WFConstants.CURRENT_TRANSITION_NAME, transitionName);
		contextInstance.setVariable(WFConstants.TASK_PROCESSED, WFConstants.STRING_YES);
		contextInstance.setVariable(WFConstants.DRAFTTIME, draftTime);

		// Creates a task instance.
		TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
		taskInstance.setActorId(requester.toString());
		processInstance.setKey(businessKey);

		jbpmContext.save(processInstance);

		// Marks this task as done and triggers a signal on the token.
		if (null != taskInstance)
		{
			if (null != transitionName)
			{
				taskInstance.end(transitionName);
			}
			else
			{
				taskInstance.end();
			}
		}

		return processInstance;
	}
	
}
